<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="关键字" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择单位状态"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.agent_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单位类型" prop="comtype">
        <el-select
          v-model="queryParams.comtype"
          placeholder="请选择单位类型"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.company_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单位等级" prop="levels">
        <el-select
          v-model="queryParams.levels"
          placeholder="请选择单位等级"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.company_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="入驻进度" prop="apstatus">
        <el-select
          v-model="queryParams.apstatus"
          placeholder="请选择入驻进度"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.apstatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="所属渠道" prop="channelId">
        <Treeselect
          ref="Treeselect"
          style="width: 250px;"
          v-model="queryParams.channelId"
          :options="channelOptions"
          :show-count="true"
          placeholder="请选择渠道"
          noResultsText="暂无数据"
          @input="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位跟进人" prop="businessUserId">
        <SelectBusinessUser
          style="width: 250px;"
          :defaultValue.sync="queryParams.businessUserId"
          :channelId="queryParams.channelId"
          :disabled="!queryParams.channelId"
          @change="handleQuery"
          placeholder="请先选择渠道,再选择单位跟进人"
        />
      </el-form-item>

      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="createTime"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5" v-hasPermi="['company:company:add']">
        <el-button
          v-if="userInfo.userType !== 2"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleOpen()"
          >新增</el-button
        >
      </el-col>
      <template v-if="userInfo.userType === 1">
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="!ids.length"
            @click="setServiceCompanyOpen"
            >批量设置服务商</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="!ids.length"
            @click="setApPrescriptionCompanyOpen"
            >批量设置审核机构</el-button
          >
        </el-col>
      </template>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['company:company:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="companyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        fixed="left"
      />
      <el-table-column
        label="单位名称"
        align="center"
        prop="cname"
        fixed="left"
        min-width="200"
      />
      <el-table-column
        label="所属行业"
        align="center"
        prop="industry"
        width="100"
      >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.industry" :value="scope.row.industry" />
        </template>
      </el-table-column>
      <el-table-column
        label="单位等级"
        align="center"
        prop="levels"
        width="100"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.company_level"
            :value="scope.row.levels"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="单位类型"
        align="center"
        prop="comtype"
        width="100"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.company_type"
            :value="scope.row.comtype"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="单位联系人"
        align="center"
        prop="contactName"
        width="100"
      />
      <el-table-column
        label="联系人电话"
        align="center"
        prop="phone"
        width="120"
      />
      <el-table-column
        label="云豆余额"
        align="center"
        prop="balance"
        width="100"
      />
      <el-table-column
        label="所属渠道"
        align="center"
        prop="channelName"
        width="200"
      />
      <el-table-column
        label="单位跟进人"
        align="center"
        prop="businessUserName"
        width="100"
      />
      <el-table-column
        label="单位服务商"
        align="center"
        prop="serviceCompanyList"
        width="180"
      >
        <template slot-scope="{ row }" v-if="row.serviceCompanyList">
          <el-tag v-for="(item,index) in row.serviceCompanyList" type="info" :key="index">{{ item.cname }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="审证审方机构"
        align="center"
        prop="apPrescriptionCompanyName"
        width="100"
      />
      <el-table-column
        label="审证审方人"
        align="center"
        prop="apPrescriptionExpertName"
        width="100"
      />
      <el-table-column
        label="单位地址"
        align="center"
        prop="address"
        min-width="180"
      />
      <el-table-column label="单位主图" align="center" prop="cimg1" width="100">
        <template slot-scope="scope">
          <image-preview
            v-if="scope.row.cimg1"
            :src="scope.row.cimg1"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="入驻进度"
        align="center"
        prop="apstatus"
        width="120"
      >
        <template slot-scope="scope">
          <div class="disFlxCC">
            <dict-tag
              :options="dict.type.apstatus"
              :value="String(scope.row.apstatus)"
            />
            <el-button
              v-if="
                userInfo.userType == 2 &&
                (scope.row.apstatus == 3 || scope.row.apstatus == 4)
              "
              @click="rowCompanyApply(scope.row)"
              size="mini"
              type="text"
              >{{
                scope.row.apstatus == 3 ? "（重新申请）" : "（申请入驻）"
              }}</el-button
            >
          </div>
        </template>
      </el-table-column> -->
      <el-table-column
        label="栏目管理"
        align="center"
        width="180"
        v-hasPermi="['company:company:edit']"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="$router.push(`/company/companyHome/index/${scope.row.id}`)"
            >主页管理</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleOpen(scope.row.id)"
            v-hasPermi="['company:company:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['company:company:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <EditCompany ref="EditCompany" @refresh="getList" />
    <BatchSetServiceCompany ref="BatchSetServiceCompany" @refresh="getList" />
    <BatchSetApPrescriptionCompany ref="BatchSetApPrescriptionCompany" @refresh="getList" />
  </div>
</template>

<script>
import { listCompany, companyApply, removeCompany } from "@/api/company/company";
import EditCompany from "./editCompany";
import BatchSetServiceCompany from "./batchSetServiceCompany";
import BatchSetApPrescriptionCompany from "./batchSetApPrescriptionCompany";
import SelectBusinessUser from "@/views/system/user/selectBusinessUser";
import { treeselect } from "@/api/channel/channel";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Company",
  dicts: [
    "agent_status",
    "industry",
    "company_level",
    "company_type",
    "apstatus",
  ],
  components: {
    EditCompany,
    BatchSetServiceCompany,
    BatchSetApPrescriptionCompany,
    Treeselect,
    SelectBusinessUser
  },
  computed: {
    userInfo() {
      return this.$store.state.user.userInfo;
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单位表格数据
      companyList: [],
      createTime: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: "",
        status: null,
        comtype: null,
        levels: null,
        apstatus: null,
        createTimeStart: null,
        createTimeEnd: null,
        channelId: null,
        businessUserId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 渠道树选项
      channelOptions: [],
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询单位列表 */
    getList() {
      this.queryParams.createTimeStart =
        this.createTime && this.createTime.length ? this.createTime[0] : "";
      this.queryParams.createTimeEnd =
        this.createTime && this.createTime.length ? this.createTime[1] : "";
      this.loading = true;
      listCompany(this.queryParams).then((response) => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询渠道下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.channelOptions = response.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
    },
    /** 新增按钮操作 */
    handleOpen(id) {
      this.$refs.EditCompany.openModal(id);
    },
    // 点击列表申请入驻
    rowCompanyApply(row) {
      this.$confirm(
        "入驻成功后，可获得更多的平台曝光量和咨询量",
        "单位入驻平台申请",
        {
          confirmButtonText: "确认申请",
          cancelButtonText: "暂不申请",
          type: "warning",
        }
      ).then(() => {
        companyApply(row.id).then((res) => {
          this.$message.success("申请成功");
          this.getList();
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "company/company/export",
        {
          ...this.queryParams,
        },
        `单位列表_${this.parseTime(new Date(), "{y}-{m}-{d}")}.xlsx`
      );
    },
    setServiceCompanyOpen() {
      this.$refs.BatchSetServiceCompany.openModal(this.ids);
    },
    setApPrescriptionCompanyOpen() {
      this.$refs.BatchSetApPrescriptionCompany.openModal(this.ids);
    },
    handleDelete(id) {
      this.$confirm("删除单位后,会同步将单位名下的科室或部门都全部删掉,请谨慎删除。", "确认要删除该单位吗？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      }).then(() => {
        removeCompany(id).then((res) => {
          this.$message.success("单位删除成功");
          this.getList();
        });
      });
    }
  },
};
</script>
